package itheima.zuoye.Test2;

public class Test {

        public static void main(String[] args) {
            // 测试调用：会导致方法内抛出“空指针异常”
            try {
                int n1 = getMax(null);
                System.out.println("最大值: " + n1);
            } catch (Exception e) {
                System.out.println("错误: " + e.getMessage());
            }

            // 测试调用：会导致方法内抛出“数组索引越界异常”
            int[] arr = new int[0];
            try {
                int n2 = getMax(arr);
                System.out.println("最大值: " + n2);
            } catch (Exception e) {
                System.out.println("错误: " + e.getMessage());
            }

            // 测试调用：正常
            int[] arr2 = {1, 2, 4, 24, 32, 5324, 32};
            try {
                int n3 = getMax(arr2);
                System.out.println("最大值: " + n3);
            } catch (Exception e) {
                System.out.println("错误: " + e.getMessage());
            }

            System.out.println("程序继续执行");
        }

        // 获取数组最大值
        public static int getMax(int[] args) {
            if (args == null) {
                throw new IllegalArgumentException("输入的数组不能为 null");
            }
            if (args.length == 0) {
                throw new IllegalArgumentException("输入的数组长度不能为 0");
            }

            int max = args[0];
            for (int i = 1; i < args.length; i++) {
                if (max < args[i]) {
                    max = args[i];
                }
            }
            return max;
        }
    }

